package net.gz01.wallet.web.controller.admin;

import com.alibaba.druid.util.StringUtils;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.jfinal.upload.UploadFile;
import net.gz01.util.ZODate;
import net.gz01.util.ZOFile;
import net.gz01.util.ZOLog;
import net.gz01.wallet.common.PageRespon;
import net.gz01.wallet.common.ZOIDGenerate;
import net.gz01.wallet.common.ZOSearchCondition;
import net.gz01.wallet.web.config.SystemConfig;
import net.gz01.wallet.web.config.SystemEnum;
import net.gz01.wallet.web.controller.BaseController;
import net.gz01.wallet.web.model.DianziZH;
import net.gz01.wallet.web.model.HeTongConfig;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class DianziZhangController extends BaseController {
    public void search(){
        render("basedata/dianziZhangList.html");
    }

    public void listJson(){
        ZOSearchCondition sc = super.getSearchCondition();
        Page<DianziZH> pageResult = DianziZH.dao.search(sc);
        renderJson(new PageRespon(pageResult));
    }

    public void addPage(){
        render("basedata/dianziZhangAdd.html");
    }

    @Before(Tx.class)
    public void batchHetong(){
        UploadFile upload = getFile();
        String title = getPara("title");
        //String keyword = getPara("keyword");

        if(StringUtils.isEmpty(title)){
            renderFail("电子章公司不能为空");
            return;
        }

//        if(StringUtils.isEmpty(keyword)){
//            renderFail("关键字不能为空");
//            return;
//        }

        ZOLog.api.info("收到数据:" + upload.getFileName() + "," + title + ",");
        DianziZH dianziZH = new DianziZH();
        dianziZH.setTitle(title);
        dianziZH.setFilename(upload.getFileName());

        String merchDataPath = SystemEnum.FilePath.PDF.getValue();
        ZOFile.mkdir(merchDataPath);
        dianziZH.setImgUrl(ZOIDGenerate.getUUID()+".jpg");
        String serverName = merchDataPath + dianziZH.getImgUrl();
        dianziZH.setUpdateTime(ZODate.getDateTime());


        BufferedImage bi = null;
        try {
            bi = ImageIO.read(upload.getFile());
        } catch (IOException e) {
            e.printStackTrace();
        }

        int width = bi.getWidth();
        int height = bi.getHeight();
        ZOLog.api.info("宽：像素-----" + width + "高：像素" + height);

        if(width!=151 || height!=151){
            renderFail("图片像素是" + width + "X" + height +";不是151像素 X 151像素");
            return;
        }


        //处理文件
        {
            upload.getFile().renameTo(new File(serverName));
            upload.getFile().delete();
        }

        dianziZH.save();
        renderSuccess();
    }


    public void look(){
        String id = getPara("id");
        DianziZH dianziZH = DianziZH.dao.selectById(id);
        redirect(SystemConfig.config.get("myUrl") + dianziZH.getImgUrl());
    }


    public void  updatePage(){
        String id = getPara("id");
        DianziZH dianziZH = DianziZH.dao.selectById(id);

        boolean ff = dianziZH.getEnable();
        dianziZH.setEnable(!ff);
        dianziZH.update();

        renderSuccess("修改成功!");

    }

}
